Completed
Push — master ( 48c879...8f1cd0 )
by Denis
02:01
created

jets.js ➔ ... ➔ options.hideBy.map   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 1
rs 10
1
/*! Jets.js - v0.13.0 - 2016-10-31
2
* http://NeXTs.github.com/Jets.js/
3
* Copyright (c) 2015 Denis Lukov; Licensed MIT */
4
5
;(function(root, definition) {
6
  if (typeof module != 'undefined') module.exports = definition();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
7
  else if (typeof define == 'function' && typeof define.amd == 'object') define(definition);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
8
  else root['Jets'] = definition();
9
}(this, function() {
10
  "use strict"
11
12
  function Jets(opts) {
13
    if( ! (this instanceof Jets)) {
14
      return new Jets(opts);
15
    }
16
    var self = this;
17
18
    var defaults = {
19
      searchSelector: '*AND',
20
      hideBy: 'display:none',
21
      diacriticsMap: {}
22
    }
23
24
    self.options = {};
25
    ['columns', 'addImportant', 'searchSelector', 'hideBy', 'manualContentHandling', 'callSearchManually', 'diacriticsMap', 'didSearch', 'invert'].forEach(function(name) {
26
      self.options[name] = opts[name] || defaults[name];
27
    });
28
    if(this.options.searchSelector.length > 1) {
29
      var searchSelector = self.options['searchSelector'].trim();
30
      self.options.searchSelector = searchSelector.substr(0, 1);
31
      self.options.searchSelectorMode = searchSelector.substr(1).toUpperCase();
32
    }
33
34
    self.content_tag = document.querySelectorAll(opts.contentTag);
35
    if( ! self.content_tag) throw new Error('Error! Could not find contentTag element');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
36
    self.content_param = opts.contentTag;
37
    self.search_tag = document.querySelector(opts.searchTag);
38
    if( ! self.search_tag && ! self.options.callSearchManually) throw new Error('Error! Provide one of search methods: searchTag or callSearchManually and call .search("phrase") manually');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
39
40
    var last_search_query = self.search_tag && self.search_tag.value || '';
41
    self.search = function(search_query) {
42
      var new_search_query = self.options.callSearchManually && typeof search_query != 'undefined'
43
        ? search_query
44
        : self.search_tag
45
          ? self.search_tag.value
46
          : ''
47
      if(last_search_query == (last_search_query = new_search_query)) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
48
      (0,self._applyCSS(last_search_query));
0 ignored issues
show
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
49
      self.options.didSearch && self.options.didSearch(last_search_query);
50
    };
51
    self._onSearch = function(event) {
52
      if(event.type == 'keydown')
53
        return setTimeout(self.search, 0);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
54
      self.search();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
55
    };
56
    self.destroy = function() {
57
      if( ! self.options.callSearchManually) self._processEventListeners('remove');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
58
      self._destroy();
59
    };
60
61
    if( ! self.options.callSearchManually) self._processEventListeners('add');
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
62
    self._addStyleTag();
63
    self._setJets();
64
    self._applyCSS(last_search_query);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
65
  }
66
67
  Jets.prototype = {
68
    constructor: Jets,
69
    _processEventListeners: function(action) {
70
      ['input', 'keydown', 'change'].forEach(function(event_type) {
71
        this.search_tag[action + 'EventListener'](event_type, this._onSearch);
72
      }.bind(this));
73
    },
74
    _applyCSS: function(search_query) {
75
      var options = this.options,
76
        search_phrase = this.replaceDiacritics(search_query.trim().toLowerCase().replace(/\s\s+/g, ' ')).replace(/\\/g, '\\\\'),
77
        words = options.searchSelectorMode
78
          ? search_phrase.split(' ').filter(function(item, pos, arr) { return arr.indexOf(item) == pos; })
79
          : [search_phrase],
80
        is_strict_selector = options.searchSelectorMode == 'AND',
81
        selectors = new Array(words.length);
82
      for(var i = 0, ii = words.length; i < ii; i++) {
83
        selectors[i] = (is_strict_selector ? this.content_param + '>' : '') + (options.invert ? '' : ':not(') + '[data-jets' +
84
          options.searchSelector + '="' + words[i] + '"]' + (options.invert ? '' : ')');
85
      }
86
      var hide_rules = options.hideBy.split(';').filter(Boolean).map(function(rule) { return rule + (options.addImportant ? '!important' : '') });
87
      var css_rule = (is_strict_selector ? '' : this.content_param + '>') + selectors.join(is_strict_selector ? ',' : '') + '{' + hide_rules.join(';') + '}';
88
      this.styleTag.innerHTML = search_phrase.length ? css_rule : '';
89
    },
90
    _addStyleTag: function() {
91
      this.styleTag = document.createElement('style');
92
      document.head.appendChild(this.styleTag);
93
    },
94
    _getText: function(tag) {
95
      return tag && (tag.textContent || tag.innerText) || '';
96
    },
97
    _getContentTags: function(query) {
98
      return Array.prototype.slice.call(this.content_tag).reduce(function(all, elem) {
99
        return all.concat(Array.prototype.slice.call(elem.querySelectorAll(query || ':scope > *')));
100
      }, []);
101
    },
102
    _setJets: function(query, force) {
103
      var self = this,
104
        tags = self._getContentTags(force ? '' : query), text;
105
      for(var i = 0, tag; tag = tags[i]; i++) {
106
        if(tag.hasAttribute('data-jets') && ! force) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
107
        text = this.options.manualContentHandling
108
          ? this.options.manualContentHandling(tag)
109
          : self.options.columns && self.options.columns.length
110
            ? self.options.columns.map(function(column) {
111
                return self._getText(tag.children[column]);
0 ignored issues
show
Bug introduced by
The variable tag is changed as part of the for loop for example by tags.i on line 105. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
112
              }).join(' ')
113
            : self._getText(tag);
114
        tag.setAttribute('data-jets', self.replaceDiacritics(text.trim().replace(/\s+/g, ' ').toLowerCase()));
115
      };
116
    },
117
    replaceDiacritics: function(text) {
118
      var diacritics = this.options.diacriticsMap;
119
      for(var letter in diacritics) if(diacritics.hasOwnProperty(letter)) {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
120
        for(var i = 0, ii = diacritics[letter].length; i < ii; i++) {
121
          text = text.replace(new RegExp(diacritics[letter][i], 'g'), letter);
122
        }
123
      }
124
      return text;
125
    },
126
    update: function(force) {
127
      this._setJets(':scope > :not([data-jets])', force);
128
    },
129
    _destroy: function() {
130
      this.styleTag.parentNode && document.head.removeChild(this.styleTag);
131
      var tags = this._getContentTags();
132
      for(var i = 0, tag; tag = tags[i]; i++) {
133
        tag.removeAttribute('data-jets');
134
      }
135
    }
136
  }
137
138
  // :scope polyfill
139
  // http://stackoverflow.com/a/17989803/1221082
140
  ;(function(doc, proto) {
141
    try {
142
      doc.querySelector(':scope body');
143
    } catch (err) {
144
      ['querySelector', 'querySelectorAll'].forEach(function(method) {
145
        var nativ = proto[method];
146
        proto[method] = function(selectors) {
147
          if (/(^|,)\s*:scope/.test(selectors)) {
148
            var id = this.id;
149
            this.id = 'ID_' + Date.now();
150
            selectors = selectors.replace(/((^|,)\s*):scope/g, '$1#' + this.id);
151
            var result = doc[method](selectors);
152
            this.id = id;
153
            return result;
154
          } else {
155
            return nativ.call(this, selectors);
156
          }
157
        }
158
      });
159
    }
160
  })(window.document, Element.prototype);
0 ignored issues
show
Bug introduced by
The variable Element seems to be never declared. If this is a global, consider adding a /** global: Element */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
161
162
  return Jets;
163
}));
164